<style scoped>

</style>
<template>
  <div>
    <x-header style="background-color:#f7fcf6;" :left-options="{backText:'',showBack: true}">添加地址</x-header>
    <group style="margin-top:0.1rem">
      <x-input title="收货人" placeholder="请输入收货人姓名" v-model="form.name" required></x-input>
      <x-input title="手机号" placeholder="请输入手机号" v-model="form.phone" required name="mobile" is-type="china-mobile"
               :min="11" :max="11"></x-input>
      <x-address :title="title" v-model="value" raw-value :list="addressData"></x-address>
      <x-input title="详细地址" placeholder="请输入详细地址" v-model="form.address" required></x-input>
      <x-switch title="是否默认" v-model="form.defaultFlag"></x-switch>
    </group>
    <x-button @click.native="addressAdd" style="margin-top:1rem">提交</x-button>
    <toast v-model="success">{{message}}</toast>
    <toast v-model="error" type="warn">{{errorMessage}}</toast>
  </div>
</template>
<script>
  import {XInput, Group, XButton, XHeader, XAddress, ChinaAddressData, XSwitch, Toast} from 'vux'
  import common from '../services/common';
  export default {
    data() {
      return {
        message: "添加成功",
        errorMessage: "添加失败",
        success: false,
        error: false,
        state: true,
        addressData: ChinaAddressData,
        title: '地址',
        value: ['上海市', '直辖区', '徐汇区'],
        form: {
          memberId: "",
          province: "",
          city: "",
          area: "",
          address: "",
          phone: "",
          defaultFlag: true,
          name: "",
          alias: ""
        }
      }
    },
    components: {
      XInput,
      Group,
      XButton,
      XHeader,
      XAddress,
      XSwitch,
      Toast
    },
    mounted: function () {
      document.title = "添加地址"
      /*if (common.checkLogin()) {
       } else {
       this.message = "正在登陆..."
       this.show1 = true;
       common.setStorage('auth_success_redirect_to', location.href);
       location.replace('http://yirenyiti.gaozhikeji.com/daiyan/wx/entrance?type=' + encodeURIComponent('auth.html?type=0'));
       this.show1 = false;
       }*/
    },
    methods: {
      addressAdd(){
        for (let i = 0; i < this.addressData.length; i++) {
          if (this.addressData[i].value == this.value[0]) {
            this.form.province = this.addressData[i].name
          } else if (this.addressData[i].value == this.value[1]) {
            this.form.city = this.addressData[i].name
          }
          else if (this.addressData[i].value == this.value[2]) {
            this.form.area = this.addressData[i].name
          }
        }
        if (this.state) {

          if (this.form.defaultFlag) {
            this.form.defaultFlag = "1"
          } else {
            this.form.defaultFlag = "0"
          }
          if (!this.form.name && this.form.name == "") {
            this.error = true;
            this.errorMessage = "请输入收货人姓名";
            return
          }
          if (!this.form.phone && this.form.phone == "") {
            this.error = true;
            this.errorMessage = "请输入手机号";
            return
          }
          if (!this.form.address && this.form.address == "") {
            this.error = true;
            this.errorMessage = "请输入详细地址";
            return
          }
          common.post(common.getRequestData("addressAdd", this.form), (res) => {
            console.log(JSON.stringify(res))
            if (res.data.responseCode == '0000') {
              this.$router.push('/order')
            } else {
              this.error = true;
              this.errorMessage = res.data.responseMsg
            }
          }, false);
        }
      }
    }
  }
</script>
